package com.bkitmobile.kesu.android.GUI;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.bkitmobile.kesu.android.ClassInfo.CmtLocation;
import com.bkitmobile.kesu.android.ClassInfo.PlaceInfo;

public class MapCommentsView extends Dialog implements View.OnClickListener{
	private TextView comments, title;
	private Button btnCmt;
	private ImageButton btnClose;
	private Context context;
	private PlaceInfo info;
	public MapCommentsView(Context context) {
		super(context);
		this.context = context;
		
	}

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.map_comment);
		//title = (TextView) findViewById(R.id.custom_dialog_title);
		comments = (TextView) findViewById(R.id.map_cmt_comments);
		btnCmt = (Button) findViewById(R.id.map_cmt_btnWrite);
		btnClose = (ImageButton) findViewById(R.id.map_cmt_btnClose);
		title = (TextView) findViewById(R.id.map_cmt_name);
		btnCmt.setOnClickListener(this);
		btnClose.setOnClickListener(this);
	}
	
	public void show(PlaceInfo info) {
		super.show();
		//title.setText(placeId);
		this.info = info;
		title.setText(info.getName());
		String cmt = "";
		int length = info.getListCmt().size();
		if(length == 0)
			cmt = "No comment.";
		else
			for(int i = 0; i < length; i++){
				CmtLocation tmp = info.getListCmt().get(i);
				cmt += formatString(tmp.getUser(), tmp.getTime(), tmp.getCmt());
			}
		comments.setText(Html.fromHtml(cmt));
		Log.d("Cmt", cmt);
		
	}
	@Override
	public void onClick(View v) {
		if(v == btnCmt)
			new MapCommentDialog(context).show(info);
		dismiss();
		
	}
	private String formatString(String atribute, String time, String content){
		String result = "";
		
		result += "<font color=\"#ffffff\"><b>" + atribute + "</b></font> (<i>" + time + "</i>)<br/>"
			   + content.replaceAll("\n", "<br/>") + "<font size=\"14\"><br/><br/></font>"
			   ;
		return result;
	}
}
